# coding:utf-8
"""
description:scene、button和station 添加或修改task
"""
import os
import random

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML
from pages.Public_elements import public_elements

TaskDevice_Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'TaskDev.yaml'))


class TaskDevice_page(public_elements):
    by = mobileby.MobileBy()

    """点击sort by一行，根据floor号筛选device"""
    TaskDeviceSort_loc = (by.ID, TaskDevice_Data.get_elementInfo(0))
    """获取device的floor and room"""
    SceneDevicefloor_loc = (by.ID, TaskDevice_Data.get_elementInfo(2))

    def func_SortFloorCancel(self, times=1):
        """
        取消floor number picker
        :return:
        """
        self.click_TaskDeviceSort()
        self.func_NumberPickerCancel(times)

    def func_SortFloor(self, indexs=0, upAndDown="up", times=1):
        """
        滑动number picker，并确定
        :param upAndDown: 向上滑还是下滑
        :param times: 滑动次数
        :return:
        """
        self.click_TaskDeviceSort()
        self.func_NumberPickerComfirm(indexs, upAndDown, times)

    def click_TaskDeviceSort(self):
        """点击sort by一行，根据floor号筛选device"""
        self.find_element(*self.TaskDeviceSort_loc).click()

    def get_TaskSortFloor(self):
        """获取筛选后的floor名"""
        return self.find_element(*self.TaskDeviceSort_loc).text

    def assert_TaskDevicefloor(self):
        """判断筛选后的 Device 房间信息是否包含楼层名"""
        floorNum = self.find_elements(*self.SceneDevicefloor_loc)

        for index, row in enumerate(floorNum):
            if self.get_TaskSortFloor() == "All Floors":  # 选择all floors，直接退出False
                return True
            else:
                if (self.get_TaskSortFloor() in row.text) == False:
                    return False
        return True

    """task列表页的device集合，点击跳转对应device操作详情页"""
    TaskDevices_loc = (by.ID, TaskDevice_Data.get_elementInfo(1))

    """点击添加其他task device page的save按钮"""
    TaskActionSave_loc = (by.ID, TaskDevice_Data.get_elementInfo(6))

    """点击ir remote control page的save按钮"""
    IRActionSave_loc = (by.ID, TaskDevice_Data.get_elementInfo(5))
    """CustomIR page的Buttons集合"""
    IrButtonBackground_loc = (by.ID, TaskDevice_Data.get_elementInfo(7))

    def click_TaskDevices(self):
        """task列表页的device集合，点击跳转对应device操作详情页"""
        random.choice(self.find_elements(*self.TaskDevices_loc)).click()

    def click_TaskActionSave(self):
        """点击添加其他task device page的save按钮"""
        self.find_element(*self.TaskActionSave_loc).click()

    def click_IRActionSave(self):
        """点击ir remote control page的save按钮"""
        self.find_element(*self.IRActionSave_loc).click()

    def assertVisible_IRActionSave(self):
        """判断IR Save按钮是否存在，存在则判定跳页为 IR　remote　control"""
        return self.assertVisible(*self.IRActionSave_loc)

    def click_IrButtonBackground(self):
        """CustomIR page的Buttons集合"""
        random.choice(self.find_elements(*self.IrButtonBackground_loc)).click()

    """brand通用，power按钮"""
    IrControlPower_loc = (by.ID, TaskDevice_Data.get_elementInfo(8))
    """air conditioner、air purifier、fan and robot vacuum通用，Mode button"""
    IrControlMode_loc = (by.ID, TaskDevice_Data.get_elementInfo(9))
    """music player、vedio player and TV通用，Return button"""
    IrControlReturn_loc = (by.ID, TaskDevice_Data.get_elementInfo(10))

    def click_IrControlPower(self):
        """brand通用，power按钮"""
        self.find_element(*self.IrControlPower_loc).click()

    def assertVisible_IrControlPower(self):
        """判断power按钮是否存在，存在则判定跳页为Brand remote control"""
        return self.assertVisible(*self.IrControlPower_loc)

    def click_IrControlMode(self):
        """air conditioner、air purifier、fan and robot vacuum通用，Mode button"""
        self.find_element(*self.IrControlMode_loc).click()

    def click_IrControlReturn(self):
        """music player、vedio player and TV通用，Return button"""
        self.find_element(*self.IrControlReturn_loc).click()

    """dimmer，1-3 gang switch，high power，plug和ceiling light通用，ON按钮"""
    PowerOn_loc = (by.ID, TaskDevice_Data.get_elementInfo(11))
    """，Off按钮"""
    PowerOff_loc = (by.ID, TaskDevice_Data.get_elementInfo(12))
    """，Toggle按钮"""
    PowerToggle_loc = (by.ID, TaskDevice_Data.get_elementInfo(13))

    def click_PowerOn(self):
        """dimmer，1-3 gang switch，high power，plug和ceiling light通用，ON按钮"""
        self.find_element(*self.PowerOn_loc).click()

    def assertVisible_PowerOn(self):
        """判断on按钮是否存在，存在则判定跳页为dimmer，1-3 gang switch，high power，plug和ceiling light"""
        return self.assertVisible(*self.PowerOn_loc)

    def click_PowerOff(self):
        """，Off按钮"""
        self.find_element(*self.PowerOff_loc).click()

    def click_PowerToggle(self):
        """，Toggle按钮"""
        self.find_element(*self.PowerToggle_loc).click()

    """1.dimmer，获取 select dim percent控件，点击或者滑动控件，不能读具体数值，最低为5%
    2.ceiling light,获取select dim控件，点击或滑动控件，不能读具体数值，最低为1%"""
    DimPercent_loc = (by.ID, TaskDevice_Data.get_elementInfo(14))

    def click_DimPercent(self):
        """dimmer，获取 select dim percent控件，点击或者滑动控件，不能读具体数值，最低为5%"""
        self.find_element(*self.DimPercent_loc).click()

    def assertVisible_DimPercent(self):
        """判断Dim Percent控件是否存在，存在则判定跳页为dimmer，ceiling light"""
        return self.assertVisible(*self.DimPercent_loc)

    """curtain和blind，获取select Action控件，点击或者滑动控件，数值在0、25、50、75、100"""
    ActionPercent_loc = (by.ID, TaskDevice_Data.get_elementInfo(15))

    def clickAndswipe_ActionPercent(self):
        """curtain和blind，获取select Action控件，点击或者滑动控件，数值在0、25、50、75、100"""
        """左滑select Action控件"""
        centre = gesture_mainpulation().centreXAndY(self.find_element(*self.ActionPercent_loc))
        self.driver.swipe(centre[2], centre[1], centre[0] - 200, centre[1], 200)

    def assertVisible_ActionPercent(self):
        """判断Action Percent控件是否存在，存在则判定跳页为curtain and blind"""
        return self.assertVisible(*self.ActionPercent_loc)

    """ceiling light，morning 按钮"""
    CeilingWake_loc = (by.ID, TaskDevice_Data.get_elementInfo(16))
    """EVening按钮"""
    CeilingSunset_loc = (by.ID, TaskDevice_Data.get_elementInfo(17))
    """Sleep按钮"""
    CeilingCandlelight_loc = (by.ID, TaskDevice_Data.get_elementInfo(18))
    """tunable按钮"""
    CeilingTunable_loc = (by.ID, TaskDevice_Data.get_elementInfo(19))
    """night light按钮"""
    CeilingNight_loc = (by.ID, TaskDevice_Data.get_elementInfo(20))
    """Color Temperature percent，2700k—6000k"""
    ColorTemp_loc = (by.ID, TaskDevice_Data.get_elementInfo(21))

    def click_CeilingWake(self):
        """ceiling light，morning 按钮"""
        self.find_element(*self.CeilingWake_loc).click()

    def click_CeilingSunset(self):
        """EVening按钮"""
        self.find_element(*self.CeilingSunset_loc).click()

    def click_CeilingCandlelight(self):
        """Sleep按钮"""
        self.find_element(*self.CeilingCandlelight_loc).click()

    def click_CeilingTunable(self):
        """tunable按钮"""
        self.find_element(*self.CeilingTunable_loc).click()

    def click_CeilingNight(self):
        """night light按钮"""
        self.find_element(*self.CeilingNight_loc).click()

    def click_ColorTemp(self):
        """Color Temperature percent，2700k—6000k"""
        self.find_element(*self.ColorTemp_loc).click()

    def assertVisible_ColorTemp(self):
        """判断ColorTemp Percent控件是否存在，存在则判定跳页为ceiling light"""
        return self.assertVisible(*self.ColorTemp_loc)

    def func_taskDevice(self, indexs=0, upAndDown="up", times=1):
        """
        随机点击task列表
        :return:
        """
        LOG.info("随机点击跳转到task详情页，根据按钮控件判断所处device页面")
        if self.assertVisible_IRActionSave():
            LOG.info("判断ir remote control page的save按钮存在。。。%s" % self.assertVisible_IRActionSave())
            if self.assertVisible_IrControlPower():
                LOG.info("判断通用brand IR　remote　control的power按钮存在。。。%s" % self.assertVisible_IrControlPower())
                """通用brand IR　remote　control"""
                self.click_IrControlPower()
                if self.assertVisible(*self.IrControlReturn_loc):
                    LOG.info("判断music player、vedio player and TV页面Return button存在。。。%s" % self.assertVisible(
                        *self.IrControlReturn_loc))
                    """Return button存在，则是music player、vedio player and TV页面"""
                    self.click_IrControlReturn()
                else:
                    LOG.info("判断air conditioner、air purifier、fan and robot vacuum页面button Mode存在。。。")
                    """否则为air conditioner、air purifier、fan and robot vacuum通用button Mode """
                    self.click_IrControlMode()
                self.click_IRActionSave()
                self.func_NumberPickerComfirm(indexs, upAndDown, times)
            else:
                """Customize IR remote control页面操作"""
                if len(self.find_elements(*self.IrButtonBackground_loc)) != 0:
                    self.click_IrButtonBackground()  # 随机点击
                self.click_IRActionSave()
                self.func_NumberPickerComfirm(indexs, upAndDown, times)
        elif self.assertVisible_PowerOn():
            LOG.info(
                "判断dimmer,1-3 gang switch，high power，plug 和ceiling light页面on 按钮存在。。。%s" % self.assertVisible_PowerOn())
            """on存在，则是dimmer,1-3 gang switch，high power，plug 和ceiling light"""
            if self.assertVisible_ColorTemp():
                LOG.info(
                    "判断ceiling light页面ColorTemp Percent控件存在存在。。。%s" % self.assertVisible_ColorTemp())

                """ColorTemp Percent控件存在，则是ceiling light"""
                nums1 = random.randint(1, 6)
                if nums1 == 1:  # 随机点击on、off and toggle
                    self.click_PowerOn()
                    if random.randint(1, 2) == 1:
                        self.click_CeilingNight()
                    else:
                        self.click_CeilingTunable()
                        # 未作light dim和color temperature的选择
                elif nums1 == 2:
                    self.click_PowerOff()
                elif nums1 == 3:
                    self.click_PowerToggle()
                elif nums1 == 4:
                    self.click_CeilingWake()
                elif nums1 == 5:
                    self.click_CeilingSunset()
                else:
                    self.click_CeilingCandlelight()
            elif self.assertVisible_DimPercent():
                LOG.info("判断dimmer页面Dim Percent按钮存在。。。%s" % self.assertVisible_DimPercent())

                """Dim Percent和on按钮存在，则是dimmer"""
                # 未作dim的选择
            else:
                nums = random.randint(1, 3)
                if nums == 1:  # 随机点击on、off and toggle
                    self.click_PowerOn()
                elif nums == 2:
                    self.click_PowerOff()
                else:
                    self.click_PowerToggle()

            self.click_TaskActionSave()

        elif self.assertVisible_ActionPercent():
            LOG.info("判断curtain 或者 blind页面Action Percent按钮存在。。。%s" % self.assertVisible_ActionPercent())
            """Action Percent控件存在，则是curtain 或者 blind"""
            # 未作dim的选择
            self.click_TaskActionSave()
        else:
            LOG.error("点击task device items跳转页面错误。。。")

    """编辑，点击有标识[ADDED]的task device"""
    AddedDevice_loc = (by.ID, TaskDevice_Data.get_elementInfo(3))

    def click_AddedDevice(self):
        """编辑，点击有标识[ADDED]的task device"""
        self.find_element(*self.AddedDevice_loc).click()
